/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.CarBean;
import DAL.Classshare;
import DAL.ExitsBean;
import java.io.*;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerCar", urlPatterns = {"/ControllerCar"})
public class ControllerCar extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        CarBean car=new CarBean();
        Classshare cs=new Classshare();
        String id=cs.GetCarID(); 
        ExitsBean exist=new ExitsBean();
        if(request.getParameter("create")!=null)
        {
            if(request.getParameter("create").equals("Create"))
            {
               String image=request.getParameter("nameimage");
                String carname=request.getParameter("carname");
                int seating=Integer.parseInt(request.getParameter("seating"));
                String conditioner=request.getParameter("conditioner");
                String driver=request.getParameter("driver");
                float PriceDate=Float.parseFloat(request.getParameter("PriceDate"));
                float Pricehour=Float.parseFloat(request.getParameter("PriceHour"));
                float Pricekilomet=Float.parseFloat(request.getParameter("Pricekilomet"));
                String CarType=request.getParameter("CarType");
                int ModelCar=Integer.parseInt(request.getParameter("ModelCar"));
                String wysiwyg=request.getParameter("wysiwyg");
                //out.println(id+":"+image+":"+carname+":"+seating+":"+conditioner+":"+driver+":");
                boolean rs=car.InsertCar(id,image,carname,seating,conditioner,driver,PriceDate,Pricehour,Pricekilomet,CarType,ModelCar,wysiwyg);
                if(rs == true)
                {
                     out.println("<script>alert('Create Car Success!');window.location='Admin/management.jsp?page=ListCars';</script>");
                }
                else
                {
                   // System.out.println("<a href='Admin/management.jsp?page=ListCars'>Create car fail back...!</a>");
                    out.println("<script>alert('Create car fail.');javascript:window.history.back(-1);</script>");
                }
          }
        }
        if(request.getParameter("delete")!=null)
        {
            if(request.getParameter("delete").equals("Delete"))
            {
                String carid=request.getParameter("carID");
                if(exist.ExitsCarIDInCarBook(carid)==true)
                {
                    out.println("<script>alert('you cannot delete! because this car has Rent customer.');window.location='Admin/management.jsp?page=CarBooking';</script>");
                }
                else
                {
                    if(car.DeleteCar(carid)== true)
                        {
                             out.println("<script>alert('Delete Car Success!');window.location='Admin/management.jsp?page=ListCars';</script>");
                        }
                        else
                        {
                           // System.out.println("<a href='Admin/management.jsp?page=ListCars'>Delete car fail back...!</a>");
                             out.println("<script>alert('Delete car fail');javascript:window.history.back(-1);</script>");
                        }
                }
            }
        }
        if(request.getParameter("edit")!=null)
        {   
            if(request.getParameter("edit").equals("Edit"))
            {
                String carid=request.getParameter("carID");
                String image=request.getParameter("image");             
                String carname=request.getParameter("carname");
                String seating=request.getParameter("seating");
                String conditioner=request.getParameter("conditioner"); 
                String driver=request.getParameter("driver");
                String PriceDate=request.getParameter("PriceDate");
                String Pricehour=request.getParameter("PriceHour");
                String Pricekilomet=request.getParameter("Pricekilomet");
                String CarType=request.getParameter("CarType");
                String ModelCar=request.getParameter("ModelCar");
                String wysiwyg=request.getParameter("description");
                
                HttpSession session=request.getSession(true);
                session.setAttribute("carid", carid);
                session.setAttribute("image", image);
                session.setAttribute("carname", carname);
                session.setAttribute("seating",seating);
                session.setAttribute("conditioner",conditioner);
                session.setAttribute("driver",driver);
                session.setAttribute("PriceDate", PriceDate);
                session.setAttribute("Pricehour", Pricehour);
                session.setAttribute("Pricekilomet", Pricekilomet);
                session.setAttribute("CarType", CarType);
                session.setAttribute("ModelCar", ModelCar);
                session.setAttribute("wysiwyg", wysiwyg);
                response.sendRedirect("Admin/management.jsp?page=EditCar");
            }
        }
         if(request.getParameter("update")!=null)
        {   
            if(request.getParameter("update").equals("Update"))
            {
                String carid=request.getParameter("CarID");
                String image="";                
                String imageDb="";
                String carname=request.getParameter("carname");
                int seating=Integer.parseInt(request.getParameter("seating"));
                String conditioner=request.getParameter("conditioner");
                String driver=request.getParameter("driver");
                float PriceDate=Float.parseFloat(request.getParameter("PriceDate"));
                float Pricehour=Float.parseFloat(request.getParameter("PriceHour"));
                float Pricekilomet=Float.parseFloat(request.getParameter("Pricekilomet"));
                String CarType=request.getParameter("CarType");
                int ModelCar=Integer.parseInt(request.getParameter("ModelCar"));
                String wysiwyg=request.getParameter("wysiwyg");
                if(request.getParameter("image")!=null)
                {
                    image=request.getParameter("image");
                    //out.println(image);
                    boolean rs=car.UpdateCarInmgae(carid,image,carname,seating,conditioner,driver,PriceDate,Pricehour,Pricekilomet,CarType,ModelCar,wysiwyg);
                    if(rs ==true)
                    {
                        HttpSession session=request.getSession(true);
                        session.removeAttribute("carid");
                        session.removeAttribute("image");
                        session.removeAttribute("carname");
                        session.removeAttribute("seating");
                        session.removeAttribute("conditioner");
                        session.removeAttribute("driver");
                        session.removeAttribute("PriceDate");
                        session.removeAttribute("Pricehour");
                        session.removeAttribute("Pricekilomet");
                        session.removeAttribute("CarType");
                        session.removeAttribute("ModelCar");
                        session.removeAttribute("wysiwyg");
                        out.println("<script>alert('Update Car Success!');window.location='Admin/management.jsp?page=ListCars';</script>");
                        
                    }
                    else
                    {
                         //System.out.println("<a href='Admin/management.jsp?page=ListCars'>Update car fail back...!</a>");
                        out.println("<script>alert('Update car fail!');javascript:window.history.back(-1);</script>");
                    }
                }
                if(request.getParameter("imageDB")!=null)
                {
                    imageDb=request.getParameter("imageDB");
                   // out.println(imageDb);
                    boolean rs=car.UpdateCarNoInmgae(carid,carname,seating,conditioner,driver,PriceDate,Pricehour,Pricekilomet,CarType,ModelCar,wysiwyg);
                     if(rs ==true)
                    {
                        HttpSession session=request.getSession(true);
                        session.removeAttribute("carid");
                        session.removeAttribute("image");
                        session.removeAttribute("carname");
                        session.removeAttribute("seating");
                        session.removeAttribute("conditioner");
                        session.removeAttribute("conditioner");
                        session.removeAttribute("PriceDate");
                        session.removeAttribute("Pricehour");
                        session.removeAttribute("Pricekilomet");
                        session.removeAttribute("CarType");
                        session.removeAttribute("ModelCar");
                        session.removeAttribute("wysiwyg");
                        out.println("<script>alert('Update Car Success!');window.location='Admin/management.jsp?page=ListCars';</script>");
                    }
                    else
                    {
                         //System.out.println("<a href='Admin/management.jsp?page=ListCars'>Update car fail back...!</a>");
                        out.println("<script>alert('Update car fail!');javascript:window.history.back(-1);</script>");
                    }
                }
            }
         }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
